<script src="../../main.js"></script>
<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-picture-outline"></i> VIP套餐列表
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                  <el-button
                        type="primary"
                        icon="el-icon-lx-roundadd"
                        class="handle-del mr10"
                        @click="addItem"
                        v-if="globalPermission.vip.edit"
                   >新增套餐
                    </el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column prop="id" label="id" width="80" align="center"></el-table-column>
                <el-table-column prop="payMoney" show-overflow-tooltip align="center" label="支付金额(RMB)"></el-table-column>
                <el-table-column prop="vipType" show-overflow-tooltip label="卡类型" align="center"></el-table-column>
                <el-table-column prop="vipDay" show-overflow-tooltip label="有效天数" align="center"></el-table-column>
                <el-table-column prop="msg" show-overflow-tooltip align="center" label="描述"></el-table-column>
                <el-table-column prop="deleted"  width="80" label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.deleted==1" type="info">下架</el-tag>
                        <el-tag v-if="scope.row.deleted==0" type="warning">上架</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="createdTime"  width="160"  label="创建时间" align="center"></el-table-column>
                <el-table-column label="操作" width="180"  align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleDelete(scope.row.id,1)"
                                v-if="globalPermission.gold.edit && scope.row.deleted ==0"
                        >下架
                        </el-button>
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleDelete(scope.row.id,0)"
                                v-if="globalPermission.gold.edit && scope.row.deleted==1"
                        >上架
                        </el-button>
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.row.id,scope.row)"
                                v-if="globalPermission.gold.edit"
                        >编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageNum"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

      <!-- 编辑弹出框 -->
    <el-dialog title="新增/编辑" :visible.sync="editVisible" width="30%">
        <el-form ref="form" :model="form" label-width="70px">
            <el-form-item label="支付金额">
                <el-input-number
                              placeholder="支付金额"
                              v-model="form.payMoney"
                              controls-position="right"
                              :min="0"
                              :max="100000"
                              @input.native="onInput0_100000"
                ></el-input-number>RMB
            </el-form-item>
            <el-form-item label="有效天数">
                <el-input-number placeholder="VIP有效期"
                              v-model="form.vipDay"
                              controls-position="right"
                              :min="0"
                              :max="100000"
                              @input.native="onInput0_100000"
                ></el-input-number>天有效
            </el-form-item>
            <el-form-item label="会员类型">
                <el-input v-model="form.vipType" placeholder="会员类型：约会卡，年卡等"></el-input>
            </el-form-item>
            <el-form-item label="购买说明">
                <el-input v-model="form.msg" placeholder="优惠说明：原价6000的卡"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="clean">取 消</el-button>
            <el-button type="primary" @click="saveEdit">确 定</el-button>
        </span>
    </el-dialog>
    </div>
</template>

<script>
    import {getAll,editModel} from '../../api/vip';
    var queryRequest = {url: "/manager/vip-gold/getVipConfig"};
    var editRequest = {url:"/manager/vip-gold/updateVipStatus"};
    var updateAddRequest = {url:"/manager/vip-gold/updateVipConfig"};
    export default {
        name: 'usertable',
        data() {
            return {
                query: {
                    title: '',
                    pageNum: 1,
                    pageSize: 10,
                },
                statusQuery:{
                    id:'',
                    status
                },
                uploadData:null,
                picData :[],
                globalPermission: this.getPermission(),
                getToken:{'auth-token':localStorage.getItem('token')},
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                editRolesVisible: false,
                formIdShow: false,
                pageTotal: 0,
                picData:[],
                form: {},
                statusForm:{},
                idx: -1,
                id: -1,
                isCurEdit:false,
            };
        },
        created() {
            queryRequest.data = this.query;
            this.getData();
        },
        methods: {
            // 获取数据
            getData() {
                //获取新闻列表
                getAll(queryRequest).then(res => {
                    this.tableData = res.data.list;
                    this.pageTotal = res.data.total;
                });
            },
            clean(){
                this.editVisible=false;
                this.handleSearch();
            },
            getPermission() {
                return JSON.parse(sessionStorage.getItem("permission"));
            },
            onInput0_100000(e) {
              this.$message.closeAll();
              // 验证是否是纯数字
              let isNumber = /^\d*$/.test(e.target.value);
              // 过滤非数字
              e.target.value = e.target.value.replace(/\D/g, "");
              if (!isNumber || e.target.value < 0 || e.target.value > 1000) {
                this.$message.warning("只能输入[0,100000]区间的整数");
              }
              e.target.value = (e.target.value >= 0 && e.target.value <= 1000 && e.target.value.match(/^\d*/g)[0]) || null;
            },

            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageNum', 1);
                this.getData();
            },
            refreshPic(response){
                this.handleSearch();
            },
            beforeUpload (file) {
                this.uploadData = {id:this.id};
                let promise = new Promise((resolve) => {
                    this.$nextTick(function () {
                        resolve(true);
                    });
                });
                return promise; //通过返回一个promis对象解决
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            // 新增
            addItem() {
                this.form={};
                this.isCurEdit = false;
                this.editVisible = true;
            },
            // 上下架操作
            handleDelete(id, status) {
                var msg = "下架";
                if(status===0){
                   msg = "上架";
                };
                // 二次确认上下架
                this.$confirm("你确定要"+msg+"?", '提示', {
                    type: 'warning'
                }).then(() => {
                    this.form.id = id;
                    this.form.status = status;
                    editRequest.data = this.form;
                    editModel(editRequest).then(res => {
                        if (res.code == 0) {
                            this.$message.success(`成功`);
                            this.editVisible = false;
                            this.handleSearch();
                        } else {
                            this.$message.error(res.message);
                        }
                    });
                }).catch(() => {
                });
            },
            // 编辑操作
            handleEdit(id, row) {
                this.id = id;
                this.form = row;
                this.form.id = this.id;
                this.editVisible = true;
            },
            // 新增
            addItem() {
                this.form={};
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                updateAddRequest.data = this.form;
                editModel(updateAddRequest).then(res => {
                    if (res.code == 0) {
                        this.$message.success(`修改成功`);
                        this.editVisible = false;
                        this.handleSearch();
                    } else {
                        this.$message.error(res.message);
                    }
                });
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageNum', val);
                this.getData();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 130px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }

    .show {
        display: block;
    }

    .hide {
        display: none;
    }

    .checkbox-rows {
        width: 130px;
        margin-top: 10px;
    }

    .el-checkbox.is-bordered + .el-checkbox.is-bordered {
        margin-left: 0px;
    }
</style>
